package com.live.shop.scene.ws;

import android.content.Context;

import com.live.shop.tool.MD5Util;

import org.greenrobot.eventbus.EventBus;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.exceptions.WebsocketNotConnectedException;
import org.java_websocket.handshake.ServerHandshake;

import java.io.FileOutputStream;
import java.net.URI;
import java.nio.ByteBuffer;

public class WsClient extends WebSocketClient {
    private static WsClient wsClient;
    public WsClient(URI serverUri) {
        super(serverUri);
    }

    //发起连接
    public static void connect(Context context, String address) {
        if (wsClient != null) {
            //释放
            Release();
        }
        if (wsClient == null ) {
            //获取时间戳
            long timestamp = System.currentTimeMillis();
            String sign = MD5Util.encrypt(context.getPackageName()+"junge"+timestamp);
            URI uri = URI.create("ws://"+address+"/getConnect?timestamp="+timestamp+"&sign="+sign);
            wsClient = new WsClient(uri);
        }
        try {
            wsClient.connectBlocking();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    //释放
    public static void Release() {
        Close();
        wsClient = null;
    }

    //断开连接
    public static void Close() {
        if (wsClient == null) return;
        if (!wsClient.isOpen()) return;
        try {
            //与关闭相同，但阻止，直到websocket关闭或未能关闭为止。
            wsClient.closeBlocking();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    //发送消息
    public static void Send(String string) {
        if (wsClient == null) return;
        if (!wsClient.isOpen())
            Reconnect();
        try {
            wsClient.send(string);
        } catch (WebsocketNotConnectedException e) {
            e.printStackTrace();
        }
    }
    //重新连接
    public static void Reconnect() {
        if (wsClient == null) return;
        if (wsClient.isOpen()) return;
        try {
            wsClient.reconnectBlocking();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //开启连接
    @Override
    public void onOpen(ServerHandshake handshakedata) {
        EventBus.getDefault().post(new WsMessageEvent(WsStatus.onOpen,"[客户端]成功连接到服务端"));
    }

    //断开连接
    @Override
    public void onClose(int code, String reason, boolean remote) {
        EventBus.getDefault().post(new WsMessageEvent(WsStatus.onClose,"[客户端]onClose：code["+code+"],reason["+reason+"]"));
    }

    //接受字节流文件
    @Override
    public void onMessage(ByteBuffer buffer) {
        EventBus.getDefault().post(new WsMessageEvent(WsStatus.onMessageByteBuffer,buffer));
    }

    //接受消息
    @Override
    public void onMessage(String message) {
        EventBus.getDefault().post(new WsMessageEvent(WsStatus.onMessageString,message));
    }

    //发生错误
    @Override
    public void onError(Exception ex) {
        EventBus.getDefault().post(new WsMessageEvent(WsStatus.onError,"[客户端]onError："+ex.toString()));
    }
}

